package com.example.controller;

import com.example.entity.HealthMetrics;
import com.example.service.HealthMetricsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/healthmetrics")
public class HealthMetricsController {

    @Autowired
    private HealthMetricsService healthMetricsService;

    @GetMapping("/list")
    public String list(Model model) {
        List<HealthMetrics> metricsList = healthMetricsService.getAllHealthMetrics();
        model.addAttribute("healthmetrics", metricsList);
        return "healthmetrics/list";
    }

    @GetMapping("/add")
    public String toAddPage() {
        return "healthmetrics/add";
    }

    @PostMapping("/add")
    public String add(
            @RequestParam("userid") int userid,
            @RequestParam("metricstype") String metricstype,
            @RequestParam("value") String value,
            @RequestParam("unit") String unit,
            @RequestParam("createtime") String createtime, // 直接接收 String 类型
            RedirectAttributes redirectAttributes
    ) {
        HealthMetrics metrics = new HealthMetrics();
        metrics.setUserid(userid);
        metrics.setMetricstype(metricstype);
        metrics.setValue(value);
        metrics.setUnit(unit);
        metrics.setCreatetime(createtime); // 直接设置 String

        int result = healthMetricsService.addHealthMetrics(metrics);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "记录添加成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "记录添加失败");
        }
        return "redirect:/healthmetrics/list";
    }
    @GetMapping("/edit/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model) {
        HealthMetrics metrics = healthMetricsService.getHealthMetricsById(id);
        model.addAttribute("metrics", metrics);
        return "healthmetrics/edit";
    }

    @PostMapping("/edit")
    public String edit(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        // 打印所有参数
        System.out.println("收到的参数：");
        request.getParameterMap().forEach((k,v) ->
                System.out.println(k + "=" + Arrays.toString(v)));

        try {
            HealthMetrics metrics = new HealthMetrics();
            metrics.setMetricsid(Integer.parseInt(request.getParameter("metricsid")));
            metrics.setUserid(Integer.parseInt(request.getParameter("userid")));
            metrics.setMetricstype(request.getParameter("metricstype"));
            metrics.setValue(request.getParameter("value"));
            metrics.setUnit(request.getParameter("unit"));
            metrics.setCreatetime(request.getParameter("createtime"));

            System.out.println("转换后的对象：" + metrics);

            int result = healthMetricsService.updateHealthMetrics(metrics);
            // ...处理结果
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("error", "参数错误：" + e.getMessage());
        }
        return "redirect:/healthmetrics/list";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id, RedirectAttributes redirectAttributes) {
        int result = healthMetricsService.deleteHealthMetrics(id);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "记录删除成功");
        } else {
            redirectAttributes.addFlashAttribute("error", "记录删除失败");
        }
        return "redirect:/healthmetrics/list";
    }
}